//
//  TravelDetailableViewCell.m
//  Travels
//
//  Created by yanghuiyan on 16/3/12.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "TravelDetailableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation TravelDetailableViewCell

- (void)dealloc {
    [_text release];
    [_pic release];
    [_imageHeight release];
    [_imageWidth release];
    [_notesModel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.text = [UILabel new];
        self.text.numberOfLines = 0;
        self.text.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:self.text];
        
        self.pic = [UIImageView new];
        [self.contentView addSubview:self.pic];
        
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //    图片布局
    //    图片高度
    CGFloat height = [[self class] heightWithImageWidth:self.imageWidth WithImageHeight:self.imageHeight];
    self.pic.frame = CGRectMake(10, 5, [UIScreen mainScreen].bounds.size.width - 20, height);
    
    //    文字布局
    self.text.frame = CGRectMake(10, self.pic.frame.origin.y + 5 + self.pic.frame.size.height, [UIScreen mainScreen].bounds.size.width - 20, [[self class] heightWithText:self.text.text]);
    
}


+ (CGFloat)heightWithImageWidth:(NSNumber *)width WithImageHeight:(NSNumber *)height {
    
    if (height == nil) {
        return 0;
    } else {
        
        return ([UIScreen mainScreen].bounds.size.width) / width.floatValue * height.floatValue;
        
    }
}


+ (CGFloat)heightWithText:(NSString *)str {
    
    if (str == nil) {
        return 0;
    }
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.height;
    
}

- (void)setNotesModel:(NotesModel *)notesModel {
    if (_notesModel != notesModel) {
        [_notesModel release];
        _notesModel = [notesModel retain];
    }
    [_pic sd_setImageWithURL:[NSURL URLWithString:notesModel.photo_url]];
    _text.text = notesModel.descriptions;
    
    _imageHeight = notesModel.photo_image_height;
    _imageWidth = notesModel.photo_image_width;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
